package com.itheima.sh.Session;

import com.itheima.sh.Util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/goodsServlet")
public class GoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Cookie history = ServletUtil.getCookie(request, "history");
        String good = request.getParameter("good");
        if (history==null){
            Cookie cookie = new Cookie("history",good);
            cookie.setMaxAge(60*60);
            response.addCookie(cookie);
        }else {
            String value = history.getValue();
            String[] split = value.split(",");
            List<String> list = Arrays.asList(split);
           if (!list.contains(good)){
               value=value+"-"+good;
              history.setValue(value);
              response.addCookie(history);
           }

        }

    }
    }

